package controller;

import entity.PageBean;
import entity.Product;
import service.ProductService;
import service.TypeService;
import service.impl.ProductServiceImpl;
import service.impl.TypeServiceImpl;
import util.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @version 1.0
 * @Author: xxf
 * @CreateDate: 2020/10/5 15:30
 */
@WebServlet("/product")
public class ProductController extends BaseController {

    private ProductService service = new ProductServiceImpl();

    /**
     * 商品展示页面
     *
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收请求参数
        String tid = request.getParameter("tid");
        String currentPage = request.getParameter("currentPage");
        String pageSize = request.getParameter("pageSize");

        //如果获取不到参数当前页数就是第一页
        if (currentPage == null || currentPage == "") {
            currentPage = "1";
        }
        //如果获取不到参数每页默认每页展示4条
        if (pageSize == null || pageSize == "") {
            pageSize = "4";
        }


        //调用业务逻辑获取pagebean对象并返回
        PageBean<Product> pageBean = service.findProductByPage(Integer.parseInt(tid), Integer.parseInt(currentPage), Integer.parseInt(pageSize));

        //将pagebean对象放入request域里
        request.setAttribute("pageBean", pageBean);

        return Constants.FORWARD + "/goodsList.jsp";
    }

    public String detail(HttpServletRequest request, HttpServletResponse response) {

        //获取参数
        String pid = request.getParameter("pid");

        //调用业务
        Product product = service.findProductById(Integer.parseInt(pid));

        //响应
        request.setAttribute("product", product);
        return Constants.FORWARD + "/goodsDetail.jsp";
    }

}
